<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pslide extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $all_session = $this->session->all_userdata();
    
        if($all_session['login'][0]['mem_type'] != 2)
        {
        	redirect('authen');
        } 

    }
    
	public function index()
	{		
		$all_session = $this->session->all_userdata();
		//////////////////////////////////////////////////////
		//////////////////// pagination //////////////////////
		//////////////////////////////////////////////////////

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		$config['base_url'] = base_url().'index.php/pslide/index';
		$config['total_rows'] = count($this->db->get('vdo')->result_array());
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		$uri = $this->uri->segment(3);
		

		//////////////////////////////////////////////////////
		///////////////////// result by //////////////////////
		//////////////////////////////////////////////////////

		if($this->input->post('result_by') == null)
		{
			$result_by = 'vdo.vdo_id';	

			if(@$all_session['result_by'] != null)
			{
				$result_by = $all_session['result_by'];
			}
			
		} else {
			$result_by = $this->input->post('result_by');
			$session['result_by'] = $result_by;
			$this->session->set_userdata($session);

		}

		//////////////////////////////////////////////////////
		//////////////////// category by /////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('category_by') == null)
		{
			$category_by = 'all';

			if(@$all_session['category_by'] != null)
			{
				$category_by = $all_session['category_by'];
			}

		} else {
			$category_by = $this->input->post('category_by');			
			$session['category_by'] = $category_by;
			$this->session->set_userdata($session);
		}

		
		

		//////////////////////////////////////////////////////
		/////////////////////// data /////////////////////////
		//////////////////////////////////////////////////////
		$mem_id = $all_session['login'][0]['mem_id'];
		$mem_type = $all_session['login'][0]['mem_type'];

		$config['total_rows'] = count($this->pvdo_model->count_vdo($result_by ,$category_by ,$mem_id ,$mem_type));
		$data['vdo'] = $this->pvdo_model->count_vdo($result_by ,$category_by ,$mem_id ,$mem_type);

		$data['all'] = count($data['vdo']);
		$data['total_rows'] = $config['total_rows'];

		$data['sum_public'] = 0;
		$data['sum_private'] = 0;
		$data['sum_ban'] = 0;
		for($i=0;$i<$data['all'];$i++)
		{
			if($data['vdo'][$i]['vdo_status'] == 0)
			{
				$data['sum_public']+=1;
			} else if ($data['vdo'][$i]['vdo_status'] == 1) {
				$data['sum_private']+=1;
			} else if ($data['vdo'][$i]['vdo_status'] == 2) {
				$data['sum_ban']+=1;
			}
		}
		$data['total_rows'] = $config['total_rows'];

		$data['category'] = $this->pvdo_model->get_category();

		$this->pagination->initialize($config); 


		$data['vdo'] = $this->pvdo_model->get_vdo($per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type);

		$data['slide'] = $this->slide_model->get_slide();

		$this->load->view('pslide',$data);

	}

	public function search()
	{
		$all_session = $this->session->all_userdata();
		//////////////////////////////////////////////////////
		//////////////////// pagination //////////////////////
		//////////////////////////////////////////////////////

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		$config['base_url'] = base_url().'index.php/pslide/search';
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		$uri = $this->uri->segment(3);

		//////////////////////////////////////////////////////
		//////////////////////// search //////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('search') == null)
		{
			if(@$all_session != null)
			{
				$input = array(
					'search' => $all_session['search']
				);
			}

		} else {			
			$session['search'] = $this->input->post('search');
			$this->session->set_userdata($session);

			$input = array(
				'search' => $this->input->post('search')
			);
		}

		//////////////////////////////////////////////////////
		///////////////////// result by //////////////////////
		//////////////////////////////////////////////////////

		if($this->input->post('result_by') == null)
		{
			$result_by = 'vdo.vdo_id';

			if(@$all_session['result_by'] != null)
			{
				$result_by = $all_session['result_by'];
			}
			
		} else {
			$result_by = $this->input->post('result_by');
			$session['result_by'] = $result_by;
			$this->session->set_userdata($session);				

		}

		//////////////////////////////////////////////////////
		//////////////////// category by /////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('category_by') == null)
		{
			$category_by = 'all';

			if(@$all_session['category_by'] != null)
			{
				$category_by = $all_session['category_by'];
			}

		} else {
			$category_by = $this->input->post('category_by');			
			$session['category_by'] = $category_by;
			$this->session->set_userdata($session);
			
		}

		

		//////////////////////////////////////////////////////
		/////////////////////// data /////////////////////////
		//////////////////////////////////////////////////////
		$mem_id = $all_session['login'][0]['mem_id'];
		$mem_type = $all_session['login'][0]['mem_type'];

		$data['vdo'] = $this->pvdo_model->count_search($input ,$result_by ,$category_by,$mem_id ,$mem_type);

		$config['total_rows'] = count($this->pvdo_model->count_search($input ,$result_by ,$category_by,$mem_id ,$mem_type));

		$data['all'] = count($data['vdo']);
		$data['total_rows'] = $config['total_rows'];

		$data['sum_public'] = 0;
		$data['sum_private'] = 0;
		$data['sum_ban'] = 0;
		for($i=0;$i<$data['all'];$i++)
		{
			if($data['vdo'][$i]['vdo_status'] == 0)
			{
				$data['sum_public']+=1;
			} else if ($data['vdo'][$i]['vdo_status'] == 1) {
				$data['sum_private']+=1;
			} else if ($data['vdo'][$i]['vdo_status'] == 2) {
				$data['sum_ban']+=1;
			}
		}


		$data['category'] = $this->pvdo_model->get_category();
		$this->pagination->initialize($config);

		$data['slide'] = $this->slide_model->get_slide();



		$data['vdo'] = $this->pvdo_model->search($input ,$per_page ,$uri ,$result_by ,$category_by,$mem_id ,$mem_type);

		$this->load->view('pslide',$data);
	}

	public function update_status()
	{

		$select_all = $this->input->post('select_all');
		$data['check_remove_slide'] = $this->slide_model->check_remove_slide();

		for($i=0;$i<count($select_all);$i++)
		{
			if($select_all[$i] != "")
			{
				$vdo = array (
					'vdo_id' => $select_all[$i]
				);
				$data['check_slide'] = $this->slide_model->check_slide($vdo);

				//////////////////////////////////////////////////////
				/////////////////////// add_slide ////////////////////
				//////////////////////////////////////////////////////
				
				if($data['check_slide'][0]['slide_status'] == 0)
				{	

					$input = array(
						'vdo_id' => $data['check_slide'][0]['vdo_id'],
						'slide_status' => 1
					);	
					$this->slide_model->update_slide($input);
					//echo 'add slide : '.$input['vdo_id'];
				} 
				
			}
		}
		redirect('pslide/set_event');
	}

	public function delete_slide()
	{
		$delete_all = $this->input->post('delete_all');

		for($i=0;$i<count($delete_all);$i++)
		{
			if($delete_all[$i] != "")
			{
				$vdo = array (
					'vdo_id' => $delete_all[$i],
					'slide_status' => 0
				);
				$this->slide_model->delete_slide($vdo);
				
			}
		}

		redirect('pslide');
	}

	public function set_event()
	{		
		$data['slide'] = $this->slide_model->get_slide();
		$this->load->view('pslide_set_event',$data);

	}

	public function set_time()
	{
		$count = count($this->slide_model->get_slide());

		for($i=0;$i<$count;$i++)
		{
			$id = $this->input->post('vdo_id_'.$i);

			$input = array(
				'vdo_id' => $id,
				'slide_start' => $this->input->post('grant_start_'.$id),
				'slide_end' => $this->input->post('grant_end_'.$id)
			);
			$this->slide_model->set_time($input);
		}

		redirect('pslide/set_event');
	}
}